﻿using System;

class ClassMatrix
{
    static void Main()
    {
        int[,] array1 = 
        {
            { 12, 63, 77, 89, 11 },
            { 39, 33, 57, 69, 181 },
            { 28, 34, 76, 95, 17 },
            { 97, 85, 27, 39, 36 },
            { 26, 54, 27, 9, 36 },
        };

        int[,] array2 = 
        {
            { 45, 524, 89, 46, 487 },
            { 96, 869, 84, 236, 78 },
            { 156, 458, 65, 62, 78 },
            { 64, 49, 93, 89, 340 },
            { 64, 49, 93, 89, 340 },
        };

        Matrix firstMatrix = new Matrix(array1);
        Matrix secondtMatrix = new Matrix(array2);

        Matrix result;

        result = firstMatrix + secondtMatrix;
        result.ToString();

        Console.WriteLine();

        result = firstMatrix - secondtMatrix;
        result.ToString();

        Console.WriteLine();

        result = firstMatrix - secondtMatrix;
        result.ToString();

        Console.WriteLine();

        result = firstMatrix * secondtMatrix;
        result.ToString();
    }

    class Matrix
    {
        int[,] array;

        public int[,] Array
        {
            get
            {
                return array;
            }
            set
            {
                array = value;
            }
        }

        public Matrix(int[,] matrix)
        {
            this.Array = matrix;
        }

        public static Matrix operator +(Matrix firstArray, Matrix secondArray)
        {
            Matrix result = new Matrix(new int[firstArray.Array.GetLength(0), secondArray.Array.GetLength(1)]);
            for (int i = 0; i < result.Array.GetLength(0); i++)
            {
                for (int j = 0; j < result.Array.GetLength(1); j++)
                {
                    result.Array[i, j] = firstArray.Array[i, j] + secondArray.Array[i, j];
                }
            }
            return result;
        }

        public static Matrix operator -(Matrix firstArray, Matrix secondArray)
        {
            Matrix result = new Matrix(new int[firstArray.Array.GetLength(0), secondArray.Array.GetLength(1)]);
            for (int i = 0; i < result.Array.GetLength(0); i++)
            {
                for (int j = 0; j < result.Array.GetLength(1); j++)
                {
                    result.Array[i, j] = firstArray.Array[i, j] - secondArray.Array[i, j];
                }
            }
            return result;
        }

        public static Matrix operator *(Matrix firstArray, Matrix secondArray)
        {
            if (firstArray.Array.GetLength(0) != secondArray.Array.GetLength(1))
            {
                throw new FormatException("Matrices cannot be multiplied");               
            }

            Matrix result = new Matrix(new int[firstArray.Array.GetLength(0), secondArray.Array.GetLength(1)]);
            for (int i = 0; i < firstArray.Array.GetLength(0); i++)
            {
                for (int j = 0; j < result.Array.GetLength(1); j++)
                {
                    result.Array[i, j] = SolveCurrentElementOfMatrix(i, j, firstArray, secondArray);
                }
            }
            return result;
        }

        public void ToString()
        {
            Matrix array = this;

            for (int i = 0; i < array.array.GetLength(0); i++)
            {
                for (int j = 0; j < array.array.GetLength(0); j++)
                {
                    Console.Write(" " + array.array[i, j]);
                }
                Console.WriteLine();
            }
        }

        private static int SolveCurrentElementOfMatrix(int indexX, int indexY, Matrix firstArray, Matrix secondArray)
        {
            int sum = 0;

            for (int i = 0; i < firstArray.Array.GetLength(1); i++)
            {
                sum += firstArray.Array[indexX, i] * secondArray.Array[i, indexY];
            }
            return sum;
        }
    }
}